package com.example.pythondemo.jsoup;

import com.example.pythondemo.model.FileIndex;
import com.example.pythondemo.model.FileNum;
import com.example.pythondemo.utils.FileKit;
import com.example.pythondemo.utils.IntKit;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class Py {
    public static Random random = new Random();

    public static void jsoupTest(String url, FileNum fileNum, FileIndex fileIndex) {
        //    解析url地址
        Document document = null;
        try {
            document = Jsoup.parse(new URL(url), 10000);
        } catch (IOException e) {
            System.out.println("第" + fileIndex.add() + "章地址" + url + "错误，请检查");
            fileNum.addFalse();
            return;
        }
        Elements script = document.getElementsByTag("script");
        String data = script.get(2).data();
        String bookName = null;
        String chapterName = null;
        try {
            bookName = data.split("bookname=\"")[1].split("\"")[0];
            chapterName = data.split("chapter_name=\"")[1].split("\"")[0];
        } catch (Exception e) {
            bookName = fileIndex.getFileName();
            chapterName = document.getElementsByTag("h1").text();
        }

        Integer index = IntKit.getInt(chapterName);
        fileIndex.index(index,url);
        //num = num.split("[^0~9]")
        //System.out.println(read_title.text());
        //获取contet的内容
        Elements content = document.getElementsByClass("content");
        //<br />
        String[] split = content.text().split("    ");
        FileKit.write(Arrays.asList(split), "D://books//" + bookName + "//" + chapterName + ".txt", "GBK", fileNum);
    }

    public static void jsoupText(String url, FileNum fileNum, List<String> lines) {
        //    解析url地址
        Document document = null;
        try {
            document = Jsoup.parse(new URL(url), 10000);
            try {
                Thread.sleep(random.nextInt(200));
            } catch (InterruptedException e) {

            }
        } catch (IOException e) {
            System.out.println(url + "地址错误，请检查");
            fileNum.addFalse();
            return;
        }
        //获取read_title的内容
        Elements read_title = document.getElementsByTag("h1");
        //System.out.println(read_title.text());
        //获取contet的内容
        Elements content = document.getElementsByClass("content");
        //<br />
        String[] split = content.text().split("    ");
        List<String> strings = Arrays.asList(split);
        lines.add("## " + read_title.text());
        lines.addAll(strings);
        //FileKit.write(Arrays.asList(split),"D://test//" + read_title.text() + ".txt","GBK",fileNum);
    }
    public static void jsoupText(String url, List<String> lines) {
        //    解析url地址
        Document document = null;
        try {
           // document = Jsoup.parse(new URL(url), 10000);
            document = Jsoup.connect(url)
                    .ignoreContentType(true)
                    .ignoreHttpErrors(true)
                    .timeout(10000)
                    .post();
            try {
                Thread.sleep(random.nextInt(200));
            } catch (InterruptedException e) {

            }
        } catch (IOException e) {
            System.out.println(e);
            System.out.println(url + "地址错误，请检查");
            return;
        }
        //获取read_title的内容
        Elements read_title = document.getElementsByTag("h1");
        //System.out.println(read_title.text());
        //获取contet的内容
        Elements content = document.getElementsByClass("content");
        //<br />
        String[] split = content.text().split("    ");
        List<String> strings = Arrays.asList(split);
        lines.add("## " + read_title.text());
        lines.addAll(strings);
        //FileKit.write(Arrays.asList(split),"D://test//" + read_title.text() + ".txt","GBK",fileNum);
    }
}
